Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Web.UI.Design
Imports System.Web.UI.Design.WebControls

<Designer(GetType(QuadroContainerDesigner))> _
<ParseChildren(False)> _
Public Class Quadro
    Inherits WebControl
    Implements INamingContainer

    Dim strTitulo As String = ""

    Dim uPosicaoX As Web.UI.WebControls.Unit
    Dim uPosicaoY As Web.UI.WebControls.Unit

    Dim strCssClassTitulo As String = ""
    Dim strCssClassContainer As String = ""
    Dim blnHabilitaDragNDrop As Boolean
    Dim blnExibirOcultar As Boolean


    Public Property Titulo() As String
        Get
            Return Me.strTitulo
        End Get
        Set(ByVal value As String)
            Me.strTitulo = value
        End Set
    End Property

    Public Property CssClassTitulo() As String
        Get
            Return Me.strCssClassTitulo
        End Get
        Set(ByVal value As String)
            Me.strCssClassTitulo = value
        End Set
    End Property

    Public Property PosicaoX() As Unit
        Get
            Return Me.uPosicaoX
        End Get
        Set(ByVal value As Unit)
            Me.uPosicaoX = value
        End Set
    End Property

    Public Property Posicaoy() As Unit
        Get
            Return Me.uPosicaoY
        End Get
        Set(ByVal value As Unit)
            Me.uPosicaoY = value
        End Set
    End Property

    Public Property HabilitarDragNDrop() As Boolean
        Get
            Return Me.blnHabilitaDragNDrop
        End Get
        Set(ByVal value As Boolean)
            Me.blnHabilitaDragNDrop = value
        End Set
    End Property

    Public Property ExibirOcultar() As Boolean
        Get
            Return Me.blnExibirOcultar
        End Get
        Set(ByVal value As Boolean)
            Me.blnExibirOcultar = value
        End Set
    End Property

    Public Property CssClassContainer() As String
        Get
            Return Me.strCssClassContainer
        End Get
        Set(ByVal value As String)
            Me.strCssClassContainer = value
        End Set
    End Property


    Protected Overrides Sub LoadViewState(ByVal savedState As Object)

        MyBase.LoadViewState(savedState(0))

        Dim dadosViewState() As Object

        dadosViewState = savedState(1)
        Try
            CType(Me.Page.FindControl("left1" & Me.ClientID), HiddenField).Value = dadosViewState(1)
            CType(Me.Page.FindControl("top1" & Me.ClientID), HiddenField).Value = dadosViewState(2)
        Catch
        End Try

    End Sub

    Protected Overrides Function SaveViewState() As Object

        Dim savedState(1) As Object

        savedState(0) = MyBase.SaveViewState()

        Dim dadosViewState(3) As Object

        Try
            dadosViewState(1) = CType(Me.Page.FindControl("left1" & Me.ClientID), HiddenField).Value
            dadosViewState(2) = CType(Me.Page.FindControl("top1" & Me.ClientID), HiddenField).Value

        Catch ex As Exception

        End Try
        


        savedState(1) = dadosViewState
        Return savedState

    End Function


    Protected Overrides Sub render(ByVal writer As HtmlTextWriter)

        If Me.blnHabilitaDragNDrop Then
            Me.Page.ClientScript.RegisterStartupScript(GetType(String), "teste", "<script>window.onload=function(){setposition()}</script>")
            'writer.WriteBeginTag("script")
            'writer.WriteAttribute("src", "js/divdragndrop.js")
            'writer.Write(">")
            'writer.WriteEndTag("script")

            Me.Page.ClientScript.RegisterHiddenField("top1" & Me.ClientID, Me.Posicaoy.ToString)
            Me.Page.ClientScript.RegisterHiddenField("left1" & Me.ClientID, Me.PosicaoX.ToString)

        End If

        writer.WriteBeginTag("div")

        writer.WriteAttribute("id", Me.ClientID)

        writer.WriteAttribute("class", Me.strCssClassTitulo)

        writer.Write("style")

        writer.Write(HtmlTextWriter.EqualsDoubleQuoteString)

        writer.WriteStyleAttribute("width", Me.Width.ToString)

        'habilita o drag

        If Me.blnHabilitaDragNDrop Then
            writer.WriteStyleAttribute("position", "absolute")
            writer.WriteStyleAttribute("top", Me.Posicaoy.ToString())
            writer.WriteStyleAttribute("left", Me.PosicaoX.ToString())
            writer.WriteStyleAttribute("cursor", "move")
        End If

        writer.Write(HtmlTextWriter.DoubleQuoteChar)

        If Me.blnHabilitaDragNDrop Then
            writer.WriteAttribute("onmousedown", "dragdrop('" & Me.ClientID & "','" & Me.ClientID & "','" & Me.Posicaoy.ToString & "','" & Me.PosicaoX.ToString & "')")
        End If

        writer.Write(">")

        writer.Write("<h2>")

        writer.Write("<span")

        If Me.blnExibirOcultar Then

            ''            output.WriteAttribute("ondblclick", "togglehide(document.getElementById('" & Me.ClientID & "'))")
            '' output.WriteAttribute("title", "Duplo clique para exibir/ocutar")

        End If

        writer.Write(">")

        writer.Write(Me.strTitulo)

        writer.Write("</span>")

        writer.Write("</h2>")

        ''DIV INTERNO
        writer.WriteBeginTag("div")
        writer.WriteAttribute("class", Me.strCssClassContainer)
        writer.Write("style")
        writer.Write(HtmlTextWriter.EqualsDoubleQuoteString)
        writer.WriteStyleAttribute("width", Unit.Pixel(Me.Width.Value - 2).ToString)
        writer.Write(HtmlTextWriter.DoubleQuoteChar)

        writer.Write(">")

        writer.WriteBeginTag("table")

        writer.WriteAttribute(" cellpadding", "0")
        writer.WriteAttribute(" cellspacing", "0")

        writer.Write(">")

        writer.Write("<tr><td valign='top'>")

        EnsureChildControls()

        RenderChildren(writer)

        writer.Write("</td></tr>")

        writer.WriteEndTag("table")

        writer.WriteEndTag("div")

        writer.WriteEndTag("div")

    End Sub

End Class



Public Class QuadroContainerDesigner
    Inherits ContainerControlDesigner

    Dim _style As Style

    Public Overrides ReadOnly Property FrameCaption() As String
        Get
            Return CType(Me.Component, Quadro).Titulo
        End Get
    End Property

    Public Overrides ReadOnly Property FrameStyle() As System.Web.UI.WebControls.Style
        Get

            If _style Is Nothing Then
                _style = New Style
                _style.Font.Name = "Verdana"
                _style.Font.Size = New FontUnit(FontSize.XXSmall)

            End If
            Return _style
        End Get
    End Property
End Class

'<Designer(GetType(QuadroContainerDesigner))> _
'<ParseChildren(False)> _
'Public Class QuadroContainer
'    Inherits WebControl
'    Implements INamingContainer


'End Class